{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(algorithm='auto', eps=1.5, leaf_size=30, metric='euclidean',\n",
       "       metric_params=None, min_samples=4, n_jobs=None, p=None)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "# eps距离阈值，min_samples核心对象在eps领域的样本数阈值\n",
    "model = DBSCAN(eps=1.5, min_samples=4)  # e邻域，minpoints的参数\n",
    "model.fit(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1, -1,  2,  0,\n",
       "        1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,\n",
       "        2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,\n",
       "        3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,\n",
       "        0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3], dtype=int64)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = model.fit_predict(data)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80\n",
      "{0, 1, 2, 3, -1}\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print(len(result))\n",
    "re_set = set(result)\n",
    "print(re_set)\n",
    "print(len(re_set))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAV+ElEQVR4nO3dX4hkZ5nH8d/T40ybJusInabFTLrKYZfBkBGWNOLgxQZHlxhjvNkL3UoIyUWjKCSswdVtWPGi2QtBc6G4NLvK4hTIgoqrRDRGh72ZLPaoMZuNI6HpHmOybZvFURiZ/OlnL6rL6a4+VX1O1fnzvud8P9Akfbqn6q2uql+9532f9z3m7gIAxGuq6gYAACZDkANA5AhyAIgcQQ4AkSPIASByr6viTm+66SZvt9tV3DUAROvixYu/dfe5weOVBHm73dba2loVdw0A0TKzzaTjDK0AQOQIcgCIHEEOAJEjyAEgcgQ5AESOIAfQ0+1K7bY0NdX7b7dbdYuQEkEOoBfaS0vS5qbk3vvv0lK9wrzGH1QEOQBpeVm6enX/satXe8froOYfVAQ5AOny5WzHY1PzDyqCHIC0sJDteGxq/kFFkAOQVlakmZn9x2ZmesfroOYfVAQ5AKnTkVZXpVZLMuv9d3W1d7wOav5BVcmmWQAC1OnUJ7gH9R/X8nJvOGVhoRfiNXm8BDmAZqjxBxVDKwAQOYIcACJHkANA5AhyAIgcQQ4AkSPIASByBDkARI4gB4DIEeQAEDmCHAAiR5ADTVLjq+Q0GUGOwpAZgan5VXKajCBHIciMANX8KjlNRpCjEGRGgGp+lZwmI8hRCDIjQDW/Sk6TEeQoBJkRoJpfJafJcgtyMztiZj81s+/kdZuIF5kRoLpfz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u7k46b2WlJb5H0lJlJvWGGn5jZ2939f0tsYq6GPd4+M7tf0t2Sznr9Fig8L+mWPd+fkPRCRW0phZkdVS/Eu+7+jarbU6B3SrrHzO6S9HpJbzCzc+5+b8XtSo0FQSUwsw1Ji+4e8q5qEzGzOyV9TtJfuft21e3Jm5m9Tr1J3LOSfi3px5L+1t2fqbRhBbFeD+TfJP2fuz9cdXvKstsjf8Td7666LVkwRo68fEHSn0l63Mx+Zmb/XHWD8rQ7kfsxSd9Tb+Lv3+sa4rveKek+Se/afT5/tttjRYDokQNA5OiRA0DkCHIAiBxBDgCRI8gBIHIEOQBEjiAHgMgR5AAQuf8HwQ0/5smR/T4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy', 'ok', 'om']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
